<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增菜单</a-button> </template
      >`
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <a-button type="link" @click="handleEdit(record)" size="small">编辑</a-button>
          <a-button type="link" @click="accessEdit(record)" size="small">权限详情</a-button>
          <a-button type="link" danger @click="handleDelete(record)" size="small">删除</a-button>
        </template>
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
    <AccessDrawer @register="AccessDrawer" @success="handleSuccess" />
  </div>
</template>

<script lang="ts">
  import { defineComponent, nextTick } from 'vue';
  import { BasicTable, useTable } from '@/components/Table';
  import { getMenuList, delMenuItem } from '@/api/system/system';
  import { useDrawer } from '@/components/Drawer';
  import { columns, searchFormSchema } from './menu.data';
  import MenuDrawer from './MenuDrawer.vue';
  import AccessDrawer from './AccessDrawer.vue';
  import { useMessage } from '@/hooks/web/useMessage';

  export default defineComponent({
    name: 'MenuManagement',
    components: { BasicTable, MenuDrawer, AccessDrawer },
    setup() {
      const { createConfirm } = useMessage();

      const [registerDrawer, { openDrawer }] = useDrawer();
      const [AccessDrawer, { openDrawer: openAccess }] = useDrawer();
      const { createMessage } = useMessage();
      const [registerTable, { reload, expandAll }] = useTable({
        title: '菜单列表',
        api: getMenuList,
        columns,
        formConfig: {
          labelWidth: 80,
          schemas: searchFormSchema,
          showAdvancedButton: false,
          actionColOptions: {
            span: 6,
            style: { marginLeft: '20px' },
          },
        },
        isTreeTable: true,
        pagination: false,
        striped: false,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        actionColumn: {
          width: 220,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function accessEdit(record: Recordable) {
        openAccess(true, {
          record,
        });
      }

      async function handleDelete(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: '提示',
          content: `确认要删除当前菜单吗?`,
          onOk: async () => {
            await delMenuItem({ ids: record.id + '' });
            createMessage.success('删除菜单成功');
            handleSuccess();
          },
        });
      }

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }
      function onFetchSuccess() {
        // 演示默认展开所有表项
        nextTick(expandAll);
      }
      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        onFetchSuccess,
        accessEdit,
        handleEdit,
        handleDelete,
        handleCreate,
        handleSuccess,
        registerDrawer,
        AccessDrawer,
      };
    },
  });
</script>
